package com.wql.crypto;

import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.interfaces.DSAPrivateKey;
import java.security.interfaces.DSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

import com.wql.crypto.common.HexUtils;

/**
 * DSA最简单的数字签名算法
 * 
 * @author wuqinglong
 */
public class DSASignUtils {
	private DSASignUtils() {
	}

	private static final String KEY_ALGORITHM = "DSA";

	private static final String SIGNATURE_ALGORITHM = "SHA1withDSA";// 1024

	private static final String PUBLIC_KEY = "DSAPublicKey";

	private static final String PRIVATE_KEY = "DSAPrivateKey";

	private static final int KEY_SIZE = 1024;

	private static final String CHARSET_NAME = "utf-8";

	/**
	 * 签名
	 * 
	 * @param data
	 * @param key
	 * @return
	 */
	public static String sign(String data, String privateKey) {
		try {
			PKCS8EncodedKeySpec pKeySpec = new PKCS8EncodedKeySpec(HexUtils.hexStr2byte(privateKey));
			KeyFactory factory = KeyFactory.getInstance(KEY_ALGORITHM);
			PrivateKey key = factory.generatePrivate(pKeySpec);
			Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
			signature.initSign(key);
			signature.update(data.getBytes(CHARSET_NAME));
			return HexUtils.byte2HexStr(signature.sign());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 验证
	 * 
	 * @param data
	 * @param publicKey
	 * @param sign
	 * @return
	 */
	public static boolean verify(String data, String publicKey, String sign) {
		try {
			X509EncodedKeySpec keySpec = new X509EncodedKeySpec(HexUtils.hexStr2byte(publicKey));
			KeyFactory factory = KeyFactory.getInstance(KEY_ALGORITHM);
			PublicKey key = factory.generatePublic(keySpec);
			Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
			signature.initVerify(key);
			signature.update(data.getBytes(CHARSET_NAME));
			return signature.verify(HexUtils.hexStr2byte(sign));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 获取公钥
	 * 
	 * @param map
	 * @return
	 */
	public static String getPublicKey(Map<String, Object> map) {
		return HexUtils.byte2HexStr(((Key) map.get(PUBLIC_KEY)).getEncoded());
	}

	/**
	 * 获取私钥
	 * 
	 * @param map
	 * @return
	 */
	public static String getPrivateKey(Map<String, Object> map) {
		return HexUtils.byte2HexStr(((Key) map.get(PRIVATE_KEY)).getEncoded());
	}

	/**
	 * 初始化密钥
	 * 
	 * @return
	 */
	public static Map<String, Object> initKey() {
		try {
			KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
			keyPairGen.initialize(KEY_SIZE);
			KeyPair keyPair = keyPairGen.generateKeyPair();
			DSAPublicKey publicKey = (DSAPublicKey) keyPair.getPublic();
			DSAPrivateKey privateKey = (DSAPrivateKey) keyPair.getPrivate();
			Map<String, Object> map = new HashMap<String, Object>(2);
			map.put(PUBLIC_KEY, publicKey);
			map.put(PRIVATE_KEY, privateKey);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		String str = "hello world!";
		Map<String, Object> map = initKey();
		String pubKey = getPublicKey(map), priKey = getPrivateKey(map);
		System.out.println("str=" + str);
		System.out.println("pubKey=" + pubKey);
		System.out.println("priKey=" + priKey);
		System.out.println("sing=" + sign(str, priKey));
		System.out.println("verify=" + verify(str, pubKey, sign(str, priKey)));
	}
}
